//
//  XMHomeNoticeView.m
//  XunMi
//
//  Created by MacBook on 2024/2/18.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMHomeNoticeView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>

@interface XMHomeNoticeView ()<SDCycleScrollViewDelegate>
@property (nonatomic, strong) SDCycleScrollView *noticeView;
@property (weak, nonatomic) IBOutlet UILabel *countLa;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *countW;

@end

@implementation XMHomeNoticeView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self addSubview:self.noticeView];
    [self.noticeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 45, 0, 50));
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    [self addGestureRecognizer:tap];
    
    kCornerRadius(_countLa, 8, 0, UIColor.clearColor);
}

- (void)setupTitles:(NSArray *)titiles unread:(NSInteger)unread {
    _noticeView.titlesGroup = titiles;

    self.countLa.hidden = unread <= 0;
    NSString *badgeStr = @"";
    if (unread < 100 && unread > 0) {
        badgeStr = [NSString stringWithFormat:@"%ld", unread];
    } else if (unread >= 100 && unread < 1000) {
        badgeStr = @"99+";
    } else if (unread >= 1000) {
        badgeStr = @"⋯";
    } else {
        badgeStr = @"";
        [self.countLa setHidden:YES];
    }
    self.countLa.text = badgeStr;
    CGFloat countW = [badgeStr widthForFont:kUIFont(10)] + 8;
    countW = MAX(16, countW);
    if (unread < 10) {
        countW = 16;
    }
    self.countW.constant = countW;
}

- (void)tap {
    if (self.tapBlock) {
        self.tapBlock();
    }
}

- (SDCycleScrollView *)noticeView {
    if (_noticeView == nil) {
        _noticeView = [[SDCycleScrollView alloc] initWithFrame:CGRectZero];
        _noticeView.delegate = self;
        _noticeView.titleLabelBackgroundColor = [UIColor xm_colorWithHexString:@"#FFFFFF" alpha:0];
        _noticeView.titleLabelTextColor = UIColor.whiteColor;
        _noticeView.titleLabelTextFont = kUIFont(12);
        _noticeView.autoScrollTimeInterval = 3.0;
        _noticeView.scrollDirection = UICollectionViewScrollDirectionVertical;
        _noticeView.onlyDisplayText = YES;
        [_noticeView disableScrollGesture];
    }
    return _noticeView;
}


- (IBAction)closeAction:(UIButton *)sender {
    [self removeFromSuperview];
}

@end
